package com.easyadmin.modules.sys.controller;

import com.github.pagehelper.PageInfo;
import com.easyadmin.common.context.UserContext;
import com.easyadmin.common.tools.OrderUtils;
import com.easyadmin.modules.sys.domain.BOrderDispatch;
import com.easyadmin.common.web.base.BaseController;
import com.easyadmin.common.web.domain.request.PageDomain;
import com.easyadmin.common.web.domain.response.Result;
import com.easyadmin.common.web.domain.response.module.ResultTable;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.easyadmin.modules.sys.service.IBOrderDispatchService;

import java.util.Arrays;

/**
 * 订单派单记录Controller
 *
 * @author mars
 * @date 2025-02-11
 */
@Api(tags = {"派单管理"})
@RestController
@RequestMapping("/sys/orderDispatch")
public class BOrderDispatchController extends BaseController {

    private String prefix = "sys/orderDispatch";

    @Autowired
    private IBOrderDispatchService bOrderDispatchService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/sys/orderDispatch/main','sys:orderDispatch:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询订单派单记录列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/sys/orderDispatch/data','sys:orderDispatch:data')")
    public ResultTable list(@ModelAttribute BOrderDispatch bOrderDispatch, PageDomain pageDomain) {
        PageInfo<BOrderDispatch> pageInfo = bOrderDispatchService.selectBOrderDispatchPage(bOrderDispatch, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增订单派单记录
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/sys/orderDispatch/add','sys:orderDispatch:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增订单派单记录
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/sys/orderDispatch/add','sys:orderDispatch:add')")
    public Result save(@RequestBody BOrderDispatch bOrderDispatch) {
        // 设置订单编号
        String orderNumber = OrderUtils.generateOrderNumber();
        bOrderDispatch.setOrderNumber(orderNumber);
        bOrderDispatch.setOrderStatus(1L);
        // 获取当前用户id
        String userId = UserContext.currentUser().getUserId();
        bOrderDispatch.setUserId(userId);
        return decide(bOrderDispatchService.save(bOrderDispatch));
    }


    /**
     * 修改订单派单记录
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/sys/orderDispatch/edit','sys:orderDispatch:edit')")
    public ModelAndView edit(String id, ModelMap map) {
        BOrderDispatch bOrderDispatch = bOrderDispatchService.getById(id);
        map.put("bOrderDispatch", bOrderDispatch);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改订单派单记录
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/sys/orderDispatch/edit','sys:orderDispatch:edit')")
    public Result update(@RequestBody BOrderDispatch bOrderDispatch) {
        return decide(bOrderDispatchService.updateById(bOrderDispatch));
    }

    /**
     * 删除订单派单记录
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/sys/orderDispatch/remove','sys:orderDispatch:remove')")
    public Result batchRemove(String ids) {
        return decide(bOrderDispatchService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除订单派单记录
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/sys/orderDispatch/remove','sys:orderDispatch:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(bOrderDispatchService.removeById(id));
    }
}
